package com.zulu.karaoke;

import java.util.Locale;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zulu.karaoke.constants.CommonConstants;
import com.zulu.karaoke.gui.FragmentAboutHelp;
import com.zulu.karaoke.gui.FragmentFullListFavourite;
import com.zulu.karaoke.gui.FragmentFullListSearch;
import com.zulu.karaoke.gui.FragmentFullListSong;
import com.zulu.karaoke.gui.custom.CustomAlertDialog;

public class MainScreen extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;
	
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_screen);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		mContext = this;
		
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		super.onBackPressed();

		final View childView = LayoutInflater.from(mContext).inflate(R.layout.custom_dialog_simple_layout, null, false);
		TextView textView = (TextView)childView.findViewById(R.id.tv_mess_content);
		textView.setText(mContext.getString(R.string.mess_exit_confirm));
		final CustomAlertDialog alert = new CustomAlertDialog(mContext, childView, true);
		alert.setTitle(mContext.getString(R.string.title_exit_confirm));
		alert.setButton(mContext.getString(R.string.button_text_ok), new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				alert.hideDialog();
				finish();
			}
		});
		alert.setButton2(mContext.getString(R.string.button_text_no), new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				alert.hideDialog();
			}
		});
	alert.showDialog();
	
	
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.favourite_menu, menu);
//		
//		menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener() {
//			
//			@Override
//			public boolean onMenuItemClick(MenuItem item) {
//				// TODO Auto-generated method stub
//				Toast.makeText(getBaseContext(), "Test menu", Toast.LENGTH_SHORT).show();
//				return false;
//			}
//		});
//		return true;
//	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			
			if(CommonConstants.TabPosition.TAB_ABOUT == position) {
				Fragment fragmentAbout = FragmentAboutHelp.getInstance();
				return fragmentAbout;
			} else if(CommonConstants.TabPosition.TAB_LIST == position) {
				Fragment fragmentList = FragmentFullListSong.getInstance();
				return fragmentList;
			} 
//			else if(CommonConstants.TabPosition.TAB_SEARCH == position) {
//				Fragment fragmentSpinner = new FragmentListSong();
//				return fragmentSpinner;
//			} 
			else if(CommonConstants.TabPosition.TAB_FAVOURITE == position) {
				Fragment fragmentFavourite = FragmentFullListFavourite.getInstance();
				return fragmentFavourite;
			}
			else {
//				Fragment fragment = new DummySectionFragment();
//				Bundle args = new Bundle();
//				args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
//				fragment.setArguments(args);
//				return fragment;
				
				Fragment fragmentSearch = FragmentFullListSearch.getInstance();
				return fragmentSearch;
			} 
		}

		@Override
		public int getCount() {
			// Show 4 total pages.
			return 4;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section_search).toUpperCase(l);
			case 1:
				return getString(R.string.title_section_song_list).toUpperCase(l);
			case 2:
				return getString(R.string.title_section_favorite).toUpperCase(l);
			case 3:
				return getString(R.string.title_section_help).toUpperCase(l);
			}
			return null;
		}
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public static class DummySectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_SECTION_NUMBER = "section_number";

		public DummySectionFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_main_screen_dummy, container, false);
			TextView dummyTextView = (TextView) rootView
					.findViewById(R.id.section_label);
			dummyTextView.setText(Integer.toString(getArguments().getInt(
					ARG_SECTION_NUMBER)));
			return rootView;
		}
	}
	
}
